package com.example.cdctestsystem.service.impl;


import com.example.cdctestsystem.mapper.ConfigMapper;
import com.example.cdctestsystem.mapper.QuestionTypeMapper;

import com.example.cdctestsystem.pojo.QuestionType;
import com.example.cdctestsystem.service.QuestionTypeService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("questionTypeService")
@Transactional(rollbackFor = Exception.class)
public class QuestionTypeServiceImpl implements QuestionTypeService {

    @Resource
    private QuestionTypeMapper questionTypeMapper;

    @Resource
    private ConfigMapper configMapper;

    //添加问题类型(节点)
    @Override
    public void insert(QuestionType object) {
        questionTypeMapper.insert(object);
    }

    //通过id，删除该id下所有的节点
    @Override
    public void delete(Integer id) {
       List<Integer> ids=questionTypeMapper.findIdsById(id);
        for (Integer id1:ids
             ) {
            questionTypeMapper.deleteById(id1);
        }
        Integer pid=questionTypeMapper.findPidById(id);
        if(pid!=0){
            questionTypeMapper.deleteByPrimaryKey(id);
        }
    }

    //通过节点的id ,修改节点的信息
    @Override
    public void updateName(Integer id, String name) {
         Integer pId=questionTypeMapper.findPidById(id);
         QuestionType questionType=new QuestionType();
         questionType.setName(name);
         questionType.setId(id);
         if(pId!=0){
             questionTypeMapper.updateByPrimaryKey(questionType);
         }
    }

    @Override
    //根据条件查出问题类型，条件为关键字或者页码
    public PageInfo<QuestionType> findAllQuestionTypes(String name,int pageNum, int pageSize) {
        //一页显示多少条
        PageHelper.startPage(pageNum,pageSize);
        List<QuestionType> questionTypes=questionTypeMapper.selectList(name);
        //默认导航页码数是8，可以通过参数2设置
        PageInfo<QuestionType> pageInfo=new PageInfo<>(questionTypes,10);
        return pageInfo;
    }

    //清空缓存
    @CacheEvict(value = "questionTypeCache",allEntries = true)
    @Override
    public void clearCache() {

    }


    @Override
    public void delete(String ids) {

    }

    //修改问题类型的排序
    @Override
    public void update(QuestionType object) {
        questionTypeMapper.updateSortById(object);
    }

    @Override
    public QuestionType findById(Integer id) {
        return null;
    }

    //查出所有问题类型
    @Override
    @Cacheable(value = "questionTypeCache",key = "'getQuestionTypes'")
    public List<QuestionType> findAll() {
        return questionTypeMapper.findAll();
    }


    @Override
    public PageInfo<QuestionType> findPage(int pageSize) {
        return null;
    }

}
